<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <script src="js/vue-router.js"></script>
    <style>
        li {
            list-style: none;
            margin: 20px 10px;
            display: inline-block;
        }

        a {
            text-decoration: none;
        }

        .show {
            width: 300px;
            height: 200px;
            background-color: #ccc;
        }
    </style>
</head>

<body>
    <div id="box">
        <nav>
            <ul>
                <li><router-link :to="{name:'home'}">home</router-link></li>
                <li><router-link :to="{name:'news'}">news</router-link></li>
                <li><router-link :to="{name:'hot'}">hot</router-link></li>
            </ul>
            <h1>编程式路由</h1>
            <button @click="toHome()">去首页</button>
            <button @click="toNews()">去新闻</button>
            <button @click="toGo()">前进</button>
            <button @click="toBack()">后退</button>
            <button @click="replace()">去首页</button>
            <div class="show">
                <router-view></router-view>
            </div>
        </nav>
    </div>
</body>

</html>
<script>
    var vm=new Vue({
        el:"#box",
        data:{},
        router:new VueRouter({
           // mode:'history', 不会显示路径盘符
            //路径信息多个
           routes:[
           {path:'/home',name:'home',component:{template:'<h1>首页</h1>'}},
           {path:'/news',name:'news',component:{template:'<h1>新闻</h1>'}},
           {path:'/hot',name:'hot',component:{template:'<h1>热门</h1>'}}
           ]
        }),
        methods:{
            toHome(){
                //字符串
                //this.$router.push("home")
                //对象
                //this.$router.push({path:'/home'})
                  //命名路由
                //this.$router.push({name:'home'})
                //携带查询参数
                this.$router.push({path:'/home',query:{id:1,username:'zhangsan'}});
                console.log("传递的参数是：",this.$route.query.id);
            },
            toNews(){
                this.$router.push({name:'news',params:{id:1,username:'lisi'}});
                console.log("传递的参数是：",this.$route.params.username);
            },
            toGo(){
                this.$router.go(1)
            },
            toBack(){
                this.$router.go(-1)
            },
            replace(){
                //跟router.push很像，唯一的不同就是，它不会向history添加新记录
                this.$router.replace("home")
            }
        }
    })

</script>